package com.xoa.service.party;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.party.GridInfoMapper;
import com.xoa.dao.party.OrgDepartmentMapper;
import com.xoa.dao.party.PropagatePositionMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.party.OrgDepartmentWithBLOBs;
import com.xoa.model.party.PropagatePosition;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宣传阵地业务处理
 * Created by 刘新婷 on 2018-01-16.
 */
@Service
public class PropagatePositionService {

    @Resource
    PropagatePositionMapper propagatePositionMapper;

    @Resource
    SysCodeMapper sysCodeMapper;
    @Resource
    OrgDepartmentMapper orgDepartmentMapper;
    @Resource
    GridInfoMapper gridInfoMapper;


    //新增
    public ToJson<PropagatePosition> addActivity(PropagatePosition record, String gridLocation){
        ToJson<PropagatePosition> json = new ToJson<>();
        if(!StringUtils.checkNull(gridLocation)){
            record.setRemark2(gridLocation);
        }
        try{
            propagatePositionMapper.insertSelective(record);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    //查询列表
    public ToJson<PropagatePosition> getActivityList(PropagatePosition record, Integer page, Integer pageSize, boolean useFlag){
        ToJson<PropagatePosition> json = new ToJson<>();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        map.put("record",record);
        //获取所有的组织信息表
        List<OrgDepartmentWithBLOBs> orgDeplist = orgDepartmentMapper.queryDepIdList(null);
        List<Integer> list1 = new ArrayList<Integer>();
        /*list1.add(record.getOrgDeptId());*/
        for(OrgDepartmentWithBLOBs o :orgDeplist){
            list1.add(o.getOrgDeptId());
        }
       /* List<Integer>   reOrg = this.reOrgId(orgDeplist, record.getOrgDeptId(), list1);*/
        map.put("reOrg", list1);
        Map<String, Object> map1 = new HashMap<String, Object>();
       /* map1.put("record",record);*/
        map1.put("reOrg", list1);
        try{
            List<PropagatePosition> list=propagatePositionMapper.getActivityList(map);
            for(PropagatePosition position :list){
                Map<String,Object> map3=new HashMap<String,Object>();
                SysCode code= sysCodeMapper.getSingleCode("TYPE_OF_ACCOUNT",position.getCommunityNo());
                if(code != null&&code.getCodeName()!=null){
                    position.setCommunityName(code.getCodeName());
                }
            }
            json.setObj(list);
            json.setTotleNum(propagatePositionMapper.selectCount(map1));
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }

    //查询列表
    public ToJson<PropagatePosition> getActivityListGroup(PropagatePosition record, Integer page, Integer pageSize, boolean useFlag){
        ToJson<PropagatePosition> json = new ToJson<>();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        map.put("record",record);
        //获取所有的组织信息表
        List<OrgDepartmentWithBLOBs> orgDeplist = orgDepartmentMapper.queryDepIdListGroup(null);
        List<Integer> list1 = new ArrayList<Integer>();
        list1.add(record.getOrgDeptId());
        List<Integer>   reOrg = this.reOrgId(orgDeplist, record.getOrgDeptId(), list1);
        map.put("reOrg", reOrg);
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("record",record);
        map1.put("reOrg", reOrg);
        try{
            List<PropagatePosition> list=propagatePositionMapper.getActivityListGroup(map);
            for(PropagatePosition position :list){
                SysCode sysCode = sysCodeMapper.getSingleCode("TYPE_OF_ACCOUNT",position.getCommunityNo() );
                if(sysCode!=null){
                    position.setCommunityName(sysCode.getCodeName());
                }
            }
            json.setObj(list);
            json.setTotleNum(propagatePositionMapper.selectCount(map1));
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }

    //根据id查询详情
    public ToJson<PropagatePosition> getActivityById(Integer pId){
        ToJson<PropagatePosition> json = new ToJson<>();
        try {
            PropagatePosition propagatePosition = propagatePositionMapper.getActivityById(pId);
            json.setObject(propagatePosition);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }

    //修改
    public ToJson<PropagatePosition> updateActivityById (PropagatePosition record, String gridLocation){
        ToJson<PropagatePosition> json = new ToJson<>();
        if(!StringUtils.checkNull(gridLocation)){
            record.setRemark2(gridLocation);
        }
        try {
            int count = propagatePositionMapper.updateByPrimaryKeySelective(record);
            if(count > 0){
                json.setFlag(0);
                json.setMsg("ok");
            }else {
                json.setMsg("err");
                json.setFlag(1);
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    //删除
    public ToJson<PropagatePosition> deleteActivityById(Integer pId){
        ToJson<PropagatePosition> json = new ToJson<>();
        try {
            propagatePositionMapper.deleteByPrimaryKey(pId);
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    public List<Integer> reOrgId(List<OrgDepartmentWithBLOBs> orgDeplist, Integer orgdepId, List<Integer> list ){
        for (OrgDepartmentWithBLOBs orgDepartmentWithBLOBs : orgDeplist){
            if (orgdepId.equals(orgDepartmentWithBLOBs.getOrgDeptParent())){
                list.add(orgDepartmentWithBLOBs.getOrgDeptId());
                reOrgId(orgDeplist, orgDepartmentWithBLOBs.getOrgDeptId(), list);
            }
        }
        return list;
    }

}
